import cv2
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import gamma

if __name__ == '__main__':
    image = cv.imread('/Users/yida/Downloads/图片/萌图/2.jpg')  # 载入图像

    HSV_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)  # 图像转化为HSV图像
    H, S, V = cv2.split(HSV_img)  # HSV图像分裂3层

    V_channel = np.mean(V)  # 求V图均值
    print(V_channel)  # 输出均值
    cv.imshow('souce image', V)
    # 非对称广义高斯开始
    aref = 1.46038
    var_l = 69.3625
    var_r = 22.0368
    miu = 181.239

    # x_l = np.linspace(0, miu, 181)
    x_l = np.arange(0, 182)

    # x_r = np.linspace(miu, 255, 1000)
    x_r = np.arange(182, 256)

    x = np.append(x_l, x_r)
    print(x)
    beta_l = var_l * (np.sqrt(gamma(1 / aref) / gamma(3 / aref)))
    beta_r = var_r * (np.sqrt(gamma(1 / aref) / gamma(3 / aref)))

    y_l = (aref / ((beta_l + beta_r) * gamma(1 / aref))) * np.exp(-((-x_l + miu) / beta_l) ** aref)
    y_r = (aref / ((beta_l + beta_r) * gamma(1 / aref))) * np.exp(-((x_r - miu) / beta_r) ** aref)
    re = np.sum(y_l) + np.sum(y_r)
    print(re)
    y_l = y_l / re
    y_r = y_r / re
    print(np.sum(y_l) + np.sum(y_r))
    y = np.append(y_l, y_r)
    # print(y)
    cdf = np.zeros(256)
    for i in range(256):
        cdf[i] = np.sum(y[:i + 1])

    # plt.plot(x_l, y_l)
    # plt.plot(x_r, y_r)
    plt.plot(x, y)
    print(x)
    print(np.sum(y))
    plt.show()
    # 非对称广义高斯结束

    plt.hist(V.ravel(), 256, [0, 255], density=1, cumulative=True, histtype='barstacked')       # 统计的v的直方图
    plt.show()
    cv.waitKey(0)
    cv.destroyAllWindows()
